<?php

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
jimport('joomla.application.component.model');  

class WrdModelMservices extends WrdModel 
{
    var $_data;
	var $_id;
	var $_total = null;
	var $_pagination = null;
	var $_limit = null;
	var $_limitstart = null;

	function __construct(){
		parent::__construct();
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		
		$this->_limit = $mainframe->getUserStateFromRequest( $context.$view.'limit', 'limit', $mainframe->getCfg('list_limit'), 0);
		$this->_limitstart = $mainframe->getUserStateFromRequest( $context.$view.'limitstart', 'limitstart', 0 );

		$this->setState('limit', $this->_limit);
		$this->setState('limitstart', $this->_limitstart);
	}

	function getData(){
		
		if (empty($this->_data)){
			$query = $this->_buildQuery();
			$this->_db->setQuery( $query, $this->_limitstart, $this->_limit );
			$this->_data = $this->_db->loadObjectList();
		}
		return $this->_data;
	}

	function getTotal(){
		
		if (empty($this->_total)){
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}
		return $this->_total;
	}

	function getPagination(){
		if (empty($this->_pagination))
		{
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->_limitstart, $this->_limit );
		}
		return $this->_pagination;
	}

	function _buildQuery(){
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');

		$orderby 			= $this->_buildContentOrderBy();
		$search 			= $mainframe->getUserStateFromRequest( $context.$view.'search','search','','string');
		$filter_outlet 		= $mainframe->getUserStateFromRequest( $context.$view.'o_code','o_code','','int');
		
		$search 			= JString::strtolower( $search );
		
		$where = array();

		if ( $search ){
			$where[] = 'c.guest_name LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false );
		}
		if ( $filter_outlet != '' ){
			$where[] = 'c.o_code = '.$filter_outlet;
		}
		
		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
		
		$query = ' 	SELECT 
					c.id, 
					c.guest_name, 
					c.vip_number, 
					outlet.name as outlet_name, 
					c.meal_period, 
					c.visit_date, 
					c.questions, 
					c.answer, 
					c.follow_up,
							
					c.created_date, 
					u.name as handle_name
					FROM #__wrd_mservices as c 
					LEFT JOIN #__users as u ON u.id = c.handle_by
					LEFT JOIN #__wrd_outlets as outlet ON ( c.o_code = outlet.code && outlet.pid != "00" )
				'
		. $where
		. $orderby;
		return $query;
	}

	function _buildContentOrderBy(){
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');

		$filter_order     = $mainframe->getUserStateFromRequest( $context.$view.'filter_order_author','filter_order','id' );
		$filter_order_Dir = $mainframe->getUserStateFromRequest( $context.$view.'filter_order_Dir',  'filter_order_Dir', '' );
		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_Dir;
		return $orderby;
	}
    
	function delete($cid = array()){
		$result = false;

		if (count( $cid )){
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid );
			$query = 'DELETE FROM #__wrd_mservices'
				. ' WHERE id IN ( '.$cids.' )';
			$this->_db->setQuery( $query );
			if(!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}

		return true;
	}
 
	function publish($cid = array(), $publish = 1){
		$user 	=& JFactory::getUser();

		if (count( $cid ))
		{
			JArrayHelper::toInteger($cid);
			$cids = implode( ',', $cid );

			$query = 'UPDATE #__wrd_mservices '
				. ' SET published = '.(int) $publish
				. ' WHERE id IN ( '.$cids.' )'
			;
			$this->_db->setQuery( $query );
			if (!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}

		return true;
	}
    
     //save to table $nametable
    function store($post,$tablename){
    	$profile = JTable::getInstance($tablename, 'TableWrd');
        $profile->load();
        $profile->bind($post);
		if ($profile->store()) {
			//$profile->reorder();
			return $profile->id;
		} else {
			return 0;
		}
  	}
  	
  	function getList(){
  		$query = 'SELECT id AS value, guest_name AS text'
		. ' FROM #__wrd_mservices WHERE published=1';

		$this->_db->setQuery( $query );
		return $this->_db->loadObjectList();
  	}

	function getUserList(){

		$query = 'SELECT a.created_id as value , b.username as text FROM #__wrd_mservices a LEFT JOIN #__users b ON a.created_id = b.id 
			GROUP BY a.created_id';
		
		$this->_db->setQuery( $query );
		return $this->_db->loadObjectList();
	}
  	
  	function getItem($id){
  		$query = 'SELECT * FROM #__wrd_mservices WHERE id = '.$id;
		$this->_db->setQuery($query);
		return $this->_db->loadObject();
  	}
  	
	function getComplain($user_id = 0,$limit_start = 0, $limit = 10,$flag = 0){
  		$query = '
  			SELECT user_id 
  			FROM #__wrd_profile
  			WHERE id = '.$user_id;
  		$this->_db->setQuery($query);
		$user_info = $this->_db->loadObject();
		$user_id = $user_info->user_id ;
		if(!empty($user_id) && ($user_id != 0)){
  			if($flag==0){
				$query = '	SELECT * 
							FROM #__wrd_mservices c'
						.' 	where ( c.published = 1 and c.user_id = '.$user_id.')'; 
				 		
				$this->_db->setQuery($query,$limit_start,$limit);
				return $this->_db->loadObjectList();
  			}else{
  				$query = 'SELECT count(c.id) as total
  							FROM #__wrd_mservices as c '
						.' where ( c.published = 1 and c.user_id = '.$user_id.')';
				$this->_db->setQuery($query);
				$tt = $this->_db->loadObject();
				return $tt->total;
  			}
  		}else{
  			return NULL;
  		}
  	}
	function getPaging( $total, $limitstart, $limit )
   	{
      	// Lets load the content if it doesn't already exist
      	jimport('joomla.html.pagination');
        $this->_pagination = new JPagination( $total, $limitstart, $limit );
         
      	return $this->_pagination;
    }
}